package com.xw.childhealth.controller;

import com.xw.childhealth.VO.DataDictionaryVO;
import com.xw.childhealth.VO.ResultVO;
import com.xw.childhealth.entity.HealthGuidance;
import com.xw.childhealth.service.HealthGuidanceService;
import com.xw.childhealth.util.ResultVOUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import java.util.ArrayList;
import java.util.Date;
import java.util.List;
import java.util.Map;

/**
 * @Description:
 * @Author: y
 * @CreateDate: 2019-11-13 08:53
 * @Version: 1.0
 */
@RequestMapping("/healthGuidance")
@RestController
public class HealthGuidanceController {

    @Autowired
    private HealthGuidanceService healthGuidanceService;


    /**
     * 查询健康指导数据
     *
     * @return
     */
    @PostMapping("/getHealthGuidance")
    public ResultVO getHealthGuidance() {

        List<DataDictionaryVO> list = new ArrayList<>();
        List<DataDictionaryVO> data = recursionData(list, 0);

        return ResultVOUtil.success(data);
    }


    private List<DataDictionaryVO> recursionData(List<DataDictionaryVO> healthGuidanceList, Integer id) {

        List<HealthGuidance> parentNode = healthGuidanceService.findByParentIdEquals(id);
        if (parentNode == null || parentNode.isEmpty()) {
            return healthGuidanceList;
        }
        parentNode.forEach(e -> {
            DataDictionaryVO dataDictionaryVO = new DataDictionaryVO();
            dataDictionaryVO.setTitle(e.getInformation());
            dataDictionaryVO.setId(e.getId());

            if (e.getInformation() != null) {
                List<DataDictionaryVO> healthGuidances = recursionData(new ArrayList<>(), e.getId());

                dataDictionaryVO.setChildren(healthGuidances.size() == 0 ? null : healthGuidances);

                healthGuidanceList.add(dataDictionaryVO);

            }

        });

        return healthGuidanceList;
    }


    /**
     * 查询父节点下面的子节点信息
     *
     * @param nodeId
     * @return
     */
    @PostMapping("/getNodeInfo/{nodeId}")
    public ResultVO getNodeInfo(@PathVariable Integer nodeId) {
        List<HealthGuidance> result = healthGuidanceService.findByParentIdEquals(nodeId);
        return ResultVOUtil.success(result);
    }


    /**
     * 更新或添加
     *
     * @param type
     * @param nodeId
     * @param code1
     * @param value1
     * @param addRecordNode
     * @param map
     * @return
     */
    @RequestMapping("/data-upd")
    public ModelAndView edit(@RequestParam(value = "type", required = false) String type, @RequestParam(value = "nodeId", required = false) Integer nodeId,
                             @RequestParam(value = "code1", required = false) Integer code1, @RequestParam(value = "value1", required = false) String value1,
                             @RequestParam(value = "addRecordNode", required = false) Integer addRecordNode, Map<String, Object> map) {
        if (type == null) {
            return new ModelAndView("/pages/admin/health-guidance", map);
        }
        if (type.equals("toUpd")) {
            HealthGuidance one = healthGuidanceService.findByIdEquals(nodeId);
            map.put("type", type);
            map.put("result", one);
        } else if (type.equals("upd")) {
            HealthGuidance one = healthGuidanceService.findByIdEquals(nodeId);
            one.setOrderNumber(code1);
            one.setDetail(value1);
            healthGuidanceService.save(one);
            return new ModelAndView("/pages/admin/health-guidance", map);
        }
        if (type.equals("toAdd")) {
            map.put("addRecordNode", addRecordNode);
        } else if (type.equals("add")) {
            healthGuidanceService.save(new HealthGuidance().setCreateTime(new Date()).setUpdateTime(new Date()).setParentId(addRecordNode).setDetail(value1).setOrderNumber(code1));
            return new ModelAndView("/pages/admin/health-guidance", map);
        }
        return new ModelAndView("/pages/admin/health-guidance-data-add", map);
    }


    /**
     * 删除
     *
     * @param id
     * @return
     */
    @RequestMapping("/data-del")
    public ModelAndView del(@RequestParam("id") Integer id) {
        HealthGuidance result = healthGuidanceService.findByIdEquals(id);
        if (result != null) {
            healthGuidanceService.delRecord(id);
        }
        return new ModelAndView("/pages/admin/health-guidance");
    }
}
